<template>
  <view class="device-card" @click="handleClick">
    <image class="device-image" :src="deviceImage" mode="aspectFill" />
    <view class="device-info">
      <text class="device-name">{{ deviceName }}</text>
      <view class="device-address">
        <image
          class="location-icon"
          src="@/static/images/location.png"
          mode="aspectFit"
        />
        <text class="address-text">{{ address || "--" }}</text>
      </view>
      <text class="device-status" :class="statusClass">{{
        formatStatus(status)
      }}</text>
    </view>
  </view>
</template>

<script lang="ts" setup>
import { computed } from "vue";
import { formatStatus } from "@/utils/index";

interface Props {
  deviceName: string;
  address: string;
  status: string;
  deviceImage: string;
}

const props = withDefaults(defineProps<Props>(), {
  deviceName: "",
  address: "",
  status: "offline",
  deviceImage: "",
});

const emit = defineEmits(["click"]);

const statusClass = computed(() => {
  return {
    "status-online": props.status === "0" || props.status === "1",
    "status-offline": props.status === "2",
    "status-error": props.status === "4",
    "status-running": props.status === "3",
  };
});

const handleClick = () => {
  emit("click");
};
</script>

<style lang="scss" scoped>
.device-card {
  background: #ffffff;
  border-radius: 16rpx;
  margin: 20rpx;
  padding: 20rpx;
  display: flex;
  box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.1);

  .device-image {
    width: 160rpx;
    height: 160rpx;
    border-radius: 8rpx;
    margin-right: 20rpx;
  }

  .device-info {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: space-between;

    .device-name {
      font-size: 32rpx;
      font-weight: bold;
      color: #333;
    }

    .device-address {
      display: flex;
      align-items: center;
      margin: 10rpx 0;
      flex-shrink: 1;
      min-width: 0;

      .location-icon {
        width: 24rpx;
        height: 24rpx;
        margin-right: 8rpx;
      }

      .address-text {
        font-size: 24rpx;
        color: #666;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
    }

    .device-status {
      align-self: flex-start;
      display: inline-flex;
      align-items: center;
      padding: 4rpx 16rpx;
      border-radius: 20rpx;
      font-size: 24rpx;
      width: fit-content;

      &.status-online {
        background: rgba(82, 196, 26, 0.1);
        color: #52c41a;
      }

      &.status-offline {
        background: rgba(170, 170, 170, 0.1);
        color: #aaaaaa;
      }

      &.status-error {
        background: rgba(255, 77, 79, 0.1);
        color: #ff4d4f;
      }

      &.status-running {
        background: rgba(24, 144, 255, 0.1);
        color: #1890ff;
      }
    }
  }
}
</style>
